package com.xkdx.caipiao;

import java.util.HashMap;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.xkdx.caipiao.fragment.MyLotterFragment;
import com.xkdx.caipiao.fragment.UserInfoFragment;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.statics.IConstants;

public class MyLotterActivity extends ThreadActivity {

	private MyLotterFragment myFragment;
	private UserInfoFragment userinfoFragment;
	private FragmentManager fragmentManager;
	public static LoginInfo loginInfo;
	public static ThreadActivity context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.fragment_user_caipiao);
		fragmentManager = getSupportFragmentManager();

		context = this;
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
	}

	private void detachFragments(FragmentTransaction transaction) {

		if (myFragment != null) {
			transaction.detach(myFragment);
		}

		if (userinfoFragment != null) {
			transaction.detach(userinfoFragment);
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		detachFragments(transaction);
		loginInfo = getLoginInfo();
		if (IConstants.FromRadioButton == 0) {

			if (loginInfo != null
					&& !(TextUtils.isEmpty(loginInfo.getLogintoken()))) {
				if (userinfoFragment == null) {

					userinfoFragment = new UserInfoFragment();

					transaction.add(R.id.user_fragment, userinfoFragment);
				} else {

					transaction.attach(userinfoFragment);
				}

			} else {

				if (myFragment == null) {

					myFragment = new MyLotterFragment(MyLotterActivity.this);

					transaction.add(R.id.user_fragment, myFragment);
				} else {

					transaction.attach(myFragment);
				}
			}
		} else {
			if (userinfoFragment != null) {
				transaction.remove(userinfoFragment);
			}

			if (loginInfo != null
					&& !(TextUtils.isEmpty(loginInfo.getLogintoken()))) {
				userinfoFragment = new UserInfoFragment();

				transaction.add(R.id.user_fragment, userinfoFragment);
			} else {
				if (myFragment != null) {
					transaction.remove(myFragment);
				}

				myFragment = new MyLotterFragment(MyLotterActivity.this);
				transaction.add(R.id.user_fragment, myFragment);
			}
		}
		transaction.commitAllowingStateLoss();

	}

	@Override
	public void startThread() {

	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {

	}

	@Override
	protected void onStop() {

		super.onStop();

		IConstants.FromRadioButton = 0;

	}

	@Override
	public void onBackClick(View view) {
		Log.i("hz", "");

	}
}
